import Vue from 'vue'
import VueRouter from 'vue-router'
// import store from '@/store'
//主页面
import LayoutMain from '@/layout/main' 
import MainCommunicate from '@/contents/MainLayout/MainCommunicate'
import MainNav from '@/contents/MainLayout/MainNav'
import MainVideo from '@/contents/MainLayout/MainVideo'

//登录注册
import LoginPage from '@/pages/LoginPage'
import RegisterPage from '@/pages/RegisterPage'
//contents布局
import LayoutFunc from '@/layout/office' 
import AmuseGrant from '@/contents/Functions/AmuseGrant'
import AmusePrize from '@/contents/Functions/AmusePrize'
import AmuseRecord from '@/contents/Functions/AmuseRecord'
import ehrDepart from '@/contents/Functions/ehrDepart'
import ApplyCar from '@/contents/Functions/ApplyCar'
import ApplyChapter from '@/contents/Functions/ApplyChapter'
import ApplyOther from '@/contents/Functions/ApplyOther'
import ApplyList from '@/contents/Functions/ApplyList'
import EHRSearch from '@/contents/Functions/EHRSearch'
import MeetingLook from '@/contents/Functions/MeetingLook'
import MeetingAdd from '@/contents/Functions/MeetingAdd'
import MeetingReserve from '@/contents/Functions/MeetingReserve'
import MeetingSearch from '@/contents/Functions/MeetingSearch'
import ApprovalRegister from '@/contents/Functions/ApprovalRegister'
import ApprovalMeeting from '@/contents/Functions/ApprovalMeeting'
import ApprovalGoods from '@/contents/Functions/ApprovalGoods'
import DiskManage from '@/contents/Functions/DiskManage'
import Disk from '@/contents/Functions/Disk'
import PersonalMeeting from '@/contents/Functions/PersonalMeeting'
import PersonalNotice from '@/contents/Functions/PersonalNotice'
import PersonalPromoted from '@/contents/Functions/PersonalPromoted'
import PersonalReserve from '@/contents/Functions/PersonalReserve'

import CommonChat from '@/contents/Functions/CommonChat'

// admin后台管理
import LayoutAdmin from '@/layout/admin'
import AdminLogin from '@/contents/Admin/AdminLogin'
import PersonAdmin from '@/contents/Admin/PersonAdmin'
import MeetingAdmin from '@/contents/Admin/MeetingAdmin'
import DepartAdmin from '@/contents/Admin/DepartAdmin'
import DiskAdmin from '@/contents/Admin/DiskAdmin'
import GrantAdmin from '@/contents/Admin/GrantAdmin'
import PrizeAdmin from '@/contents/Admin/PrizeAdmin'


Vue.use(VueRouter)

const routes = [
  {
    path: '/office',
    name: 'office',
    component: LayoutFunc,
    children: [
        {
          path: 'grant',
          name: 'amuseGrant',
          component: AmuseGrant
        },
      
        {
          path: 'prize',
          name: 'amusePrize',
          component: AmusePrize
        },
      
        {
          path: 'record',
          name: 'amuseRecord',
          component: AmuseRecord
        },

        {
            path: 'depart',
            name: 'ehrDepart',
            component: ehrDepart
        },
      
        {
          path: 'car',
          name: 'applyCar',
          component: ApplyCar
        },
        {
          path: 'chapter',
          name: 'applyChapter',
          component: ApplyChapter
        },
        {
          path: 'other',
          name: 'applyOther',
          component: ApplyOther
        },
        {
            path: 'applylist',
            name: 'applyList',
            component: ApplyList
        },
        {
          path: 'search',
          name: 'eHRSearch',
          component: EHRSearch
        },
      
        {
          path: 'look',
          name: 'meetingLook',
          component: MeetingLook
        },
        {
            path: 'addMeeting',
            name: 'meetingAdd',
            component: MeetingAdd
        },

        {
            path: 'reserveMeeting',
            name: 'meetingReserve',
            component: MeetingReserve
        },

        {
            path: 'searchMeeting',
            name: 'meetingSearch',
            component: MeetingSearch
        },

        {
            path: 'registerApproval',
            name: 'approvalRegister',
            component: ApprovalRegister
        },

        {
            path: 'meetingApproval',
            name: 'approvalMeeting',
            component: ApprovalMeeting
        },

        {
            path: 'goods',
            name: 'approvalGoods',
            component: ApprovalGoods
        },

        {
            path: 'disk',
            name: 'disk',
            component: Disk,
        },

        {
            path: 'diskManage',
            name: 'diskManage',
            component: DiskManage,
        },

        {
          path: 'meeting',
          name: 'personalMeeting',
          component: PersonalMeeting
        },
      
        {
          path: 'notice',
          name: 'personalNotice',
          component: PersonalNotice
        },
      
        {
          path: 'promoted',
          name: 'personalPromoted',
          component: PersonalPromoted
        },
      
        {
          path: 'reserve',
          name: 'personalReserve',
          component: PersonalReserve
        },

        {
          path: 'chat',
          name: 'commonChat',
          component: CommonChat
        }
    ]
  },
  {
      path: '/admin',
      name: 'admin',
      component: LayoutAdmin,
      children: [
          {
              path: 'personAdmin',
              name: 'personAdmin',
              component: PersonAdmin
          },
          {
              path: 'meetingAdmin',
              name: 'meetingAdmin',
              component: MeetingAdmin
          },
          {
              path: 'departAdmin',
              name: 'departAdmin',
              component: DepartAdmin
          },
          {
              path: 'diskAdmin',
              name: 'diskAdmin',
              component: DiskAdmin
          },
          {
              path: 'grantAdmin',
              name: 'grantAdmin',
              component: GrantAdmin
          },
          {
              path: 'prizeAdmin',
              name: 'prizeAdmin',
              component: PrizeAdmin
          }
      ]
  },
  {
    path: '/main',
    name: 'main',
    component: LayoutMain,
    children: [
        {
          path: 'communicate',
          name: 'mainCommunicate',
          component: MainCommunicate
        },
        {
          path: 'nav',
          name: 'mainNav',
          component: MainNav
        },
        {
          path: 'video',
          name: 'mainVideo',
          component: MainVideo
        },
    ]
  },
  {
    path: '/login',
    name: 'LoginPage',
    component: LoginPage,
  },
  {
     path: '/login/admin',
     name: 'LoginAdmin',
     component: AdminLogin,
  },
  {
    path: '/register',
    name: 'RegisterPage',
    component: RegisterPage,
  },
  {
      path: '/',
      redirect: '/login',
  }

  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  routes
})

// 不登录就可以放行的路径
const whiteList = ['/login', '/register','/login/admin']

router.beforeEach((to, from, next) => {
  if(whiteList.includes(to.path)) { //白名单直接放行
    next()
  } else { // 验证是否有token
    const username = localStorage.getItem('username')
    const roles = Number(localStorage.getItem('role'))
    if(!roles || !username) { // 没有用户信息跳转到登录
      next('/login')
    } else { // 有用户信息
      if(to.path.search('/admin') === 0) {
        if(roles === 3) {
          next()
        } else {
          if(from.path.search('/admin') === -1)
            next(from.path)
          else 
            next('/main/nav')
        }
      } else {
        if(to.path.search('/main') === 0 || to.path.search('/office') === 0) {
          // 到功能页
          if(roles === 3) {
            localStorage.removeItem('username')
            localStorage.removeItem('role')
            next(from.path)
          } else {
            next()
          }
        }
      }
    }
  }
})

export default router
